package com.easy.cloud.web.oauth.aspect;

import com.easy.cloud.web.component.core.response.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Component;

/**
 * OpenFeign AOP
 *
 * @author GR
 * @date 2024/2/21 18:36
 */
@Slf4j
@Aspect
@Component
public class TokenEndpointAspect {

    /**
     * AOP TokenEndpoint post方法
     *
     * @param proceedingJoinPoint 切点
     * @return
     * @throws Throwable
     */
    @Around("execution(* org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.*AccessToken(..))")
    public Object point(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object proceed = proceedingJoinPoint.proceed();
        if (proceed instanceof ResponseEntity) {
            ResponseEntity<OAuth2AccessToken> result = (ResponseEntity<OAuth2AccessToken>) proceed;
            return new ResponseEntity(HttpResult.ok(result.getBody()), result.getHeaders(), HttpStatus.OK);
        }

        return proceed;
    }
}
